package com.act.moudles.act.controller;

import com.act.components.UserComponent;
import com.act.constant.LoginTypeEnums;
import com.act.constant.UserTypeEnums;
import com.act.moudles.act.pojo.TaskRunPojo;
import com.act.moudles.act.pojo.TaskSearchPojo;
import com.act.moudles.act.service.TaskListService;
import com.act.moudles.act.service.TaskRunService;
import com.act.moudles.act.vo.TaskLog;
import com.act.moudles.user.entity.User;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: mengxiangzhong
 * @date: 2023/5/16 16:00
 * @TODO:
 **/

@RestController
@RequestMapping("act/taskList")
@Api(tags = "运行任务")
public class TaskListController {

    @Resource
    private TaskListService taskService;

    @Resource
    private TaskRunService taskRunService;

    @Resource
    private UserComponent userComponent;

    @PostMapping("findStartParam")
    @ApiOperation("查询流程启动参数")
    public DataResult findStartParam(@RequestBody String json){
        JSONObject jsonObject = JSON.parseObject(json);
        try {
            String userFlowKey = jsonObject.getString("userFlowKey");
            return DataResult.success("查询成功！",taskService.findStartParams(userFlowKey,userComponent.getCurrentUser(LoginTypeEnums.WEB.getType())));
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

    @PostMapping("startFlow")
    @ApiOperation("启动流程")
    public DataResult startFlow(@RequestBody String json){
        JSONObject obj = JSON.parseObject(json);
        String userflowKey = obj.getString("userFlowKey");
        JSONArray list = obj.getJSONArray("params");

        Map<String,Object> paramMap = new HashMap<>();
        for(Object jsonObject : list){
            Map<String,Object> map = ( Map<String,Object>)jsonObject;
            paramMap.put(map.get("key").toString(),map.get("value"));
        }
        //发起流程
        try {
            User user = userComponent.getCurrentUser(LoginTypeEnums.WEB.getType());
            String procId  = taskRunService.startFlow(TaskRunPojo.builder().userFlowKey(userflowKey).paramMap(paramMap).build(),user);
            return DataResult.success("流程发起成功，流程ID："+procId,procId);
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

    @PostMapping("findAssStart")
    @ApiOperation("查询发起任务")
    public DataResult findAssStart(@RequestBody PageSearch<TaskSearchPojo> page){
        try {
            User user = userComponent.getCurrentUser(LoginTypeEnums.WEB.getType());
            TaskSearchPojo pojo = page.getDomain();
            if(user.getUserType() - UserTypeEnums.SYSTEM.getUserType() != 0L){
                pojo.setTenantId(user.getTenantId());
            }
            return DataResult.success("查询成功！",taskService.findAssStartTask(page,pojo));
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }


    @PostMapping("findTodoTask")
    @ApiOperation("查询代办任务")
    public DataResult findTodoTask(@RequestBody PageSearch<TaskSearchPojo> page){
        try {
            User user = userComponent.getCurrentUser(LoginTypeEnums.WEB.getType());
            TaskSearchPojo pojo = page.getDomain();
            if(user.getUserType() - UserTypeEnums.SYSTEM.getUserType() != 0L){
                pojo.setTenantId(user.getTenantId());
            }
            return DataResult.success("查询成功！",taskService.findAssTodoTask(page,pojo));
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }


    @PostMapping("findhasTask")
    @ApiOperation("查询已办任务")
    public DataResult findhasTask(@RequestBody PageSearch<TaskSearchPojo> page){
        try {
            User user = userComponent.getCurrentUser(LoginTypeEnums.WEB.getType());
            TaskSearchPojo pojo = page.getDomain();
            if(user.getUserType() - UserTypeEnums.SYSTEM.getUserType() != 0L){
                pojo.setTenantId(user.getTenantId());
            }
            return DataResult.success("查询成功！",taskService.findAssHasTask(page,pojo));
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

    @PostMapping("findLog")
    @ApiOperation("查询日志")
    public DataResult findLog(@RequestBody String json){
        try {
            JSONObject obj = JSON.parseObject(json);
            List<TaskLog> taskLogList = taskService.listLog(obj.getString("procId"),
                    obj.getString("userFlowKey"),userComponent.getCurrentUser(LoginTypeEnums.WEB.getType()));
            return DataResult.success("查询成功！",taskLogList);
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

}
